package com.dirk.happie.adapters;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dirk.happie.R;
import com.dirk.happie.beans.ArticleBean;
import com.dirk.happie.beans.OptionsBean;
import com.dirk.happie.utils.Common;
import com.dirk.happie.utils.ImageDownloader;
import com.dirk.happie.utils.Keys;

public class ArticlesListAdapter extends ArrayAdapter<ArticleBean> {

	Context context;
	int layoutResourceId;
	ImageDownloader imageDownloader = null;
	private String cacheFolderPath = "";
	private LayoutInflater inflater = null;

	public ArticlesListAdapter(Context context, int layoutResourceId, ArrayList<ArticleBean> data) {
		super(context, layoutResourceId, data);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		cacheFolderPath = Environment.getExternalStorageDirectory().getPath() + "/happie_cache";
		File file = new File(cacheFolderPath);
		file.mkdirs();
		imageDownloader = new ImageDownloader();
		inflater = ((Activity) context).getLayoutInflater();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ProductTypeHolder holder = null;

		if (row == null) {

			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new ProductTypeHolder();
			holder.txtTitle = (TextView) row.findViewById(R.id.txtTitle);
			holder.txtSpecs = (TextView) row.findViewById(R.id.txtSpecs);
			holder.image = (ImageView) row.findViewById(R.id.imgProduct);
			holder.image.setScaleType(ScaleType.CENTER_INSIDE);
			holder.priceNormal = (TextView) row.findViewById(R.id.txtPriceNorm);
			holder.priceNow = (TextView) row.findViewById(R.id.txtPriceNow);
			holder.special = (RelativeLayout) row.findViewById(R.id.relPriceNow);

			row.setTag(holder);
		} else {
			holder = (ProductTypeHolder) row.getTag();
			holder.special = (RelativeLayout) row.findViewById(R.id.relPriceNow);
			holder.special.setVisibility(View.GONE);
		}

		ArticleBean curArticle = this.getItem(position);
		holder.txtTitle.setText(curArticle.getArticleName());
		holder.txtSpecs.setText(curArticle.getArticleDescription());
		if (curArticle.getArticlePicture() != null) {
			imageDownloader.download(Keys.IMAGES_URL + curArticle.getArticlePicture(), cacheFolderPath + "/" + Common.getMD5(curArticle.getArticlePicture()), holder.image);
		}

		double price = 0;
		try {
			price = Float.parseFloat(Common.getPrice(curArticle.getArticlePrice()));
		} catch (Exception e) {
		}

		if (curArticle.getOption() != null && curArticle.getOption().size() > 0) {
			for (OptionsBean option : curArticle.getOption()) {
				try {
					Float optPrice = Float.parseFloat(Common.getPrice(option.getOptionPrice()));
					if (optPrice < price) {
						price = optPrice;
						curArticle.setArticlePrice(option.getOptionPrice());
					}
				} catch (Exception e) {
				}
			}
			holder.priceNormal.setText(getString(R.string.price_from) + " " + getString(R.string.currency) + " " + Common.getPrice(curArticle.getArticlePrice()));
		} else {

			holder.priceNormal.setText(getString(R.string.currency) + " " + Common.getPrice(curArticle.getArticlePrice()));
		}
		if (Common.compareOfferDate(curArticle)) {
			holder.special.setVisibility(View.VISIBLE);
			holder.priceNow.setText(getString(R.string.currency) + " " + Common.getPrice(curArticle.getOfferPrice()));
		}

		return row;
	}

	private String getString(int resId) {
		return context.getString(resId);
	}

	static class ProductTypeHolder {
		TextView txtTitle;
		TextView txtSpecs;
		ImageView image;
		TextView priceNormal;
		TextView priceNow;
		RelativeLayout special;

	}

	public void clearResources() {
		imageDownloader.clearImages();
	}
}
